package com.lap.auth.adapter.driving.query.user.repository;

import com.lap.auth.adapter.driving.query.user.UserQueryConverter;
import com.lap.auth.adapter.driving.query.user.mapper.UserQueryMapper;
import com.lap.auth.adapter.driving.query.user.po.UserSimplePo;
import com.lap.auth.adapter.driving.query.user.po.UserWithOrgPo;
import com.lap.auth.application.user.dto.*;
import com.lap.auth.application.user.repository.UserQueryRepository;
import com.lap.framework.dto.page.PageResponse;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class UserQueryRepositoryImpl implements UserQueryRepository {

  private final UserQueryConverter converter;
  private final UserQueryMapper userQueryMapper;

  @Override
  public PageResponse<UserWithOrgDto> findWithPostPaged(QueryUserPageQuery query) {
    long count = userQueryMapper.countWithOrgPage(query);
    if (count == 0) {
      return PageResponse.ok(count);
    }
    List<UserWithOrgPo> list = userQueryMapper.selectWithOrgPage(query);
    return PageResponse.ok(converter.toDtoList(list), count);
  }

  @Override
  public PageResponse<UserSimpleDto> findSimplePaged(QueryUserSimplePageQuery query) {
    long count = userQueryMapper.countSimplePage(query);
    if (count == 0) {
      return PageResponse.ok(count);
    }
    List<UserSimplePo> list = userQueryMapper.selectSimplePage(query);
    return PageResponse.ok(converter.toSimplesDtoList(list), count);
  }

  @Override
  public List<UserSimpleDto> findSimpleByIds(Set<Integer> set) {
    if (CollectionUtils.isEmpty(set)) {
      return Collections.emptyList();
    }
    List<UserSimplePo> list = userQueryMapper.selectSimpleListByIds(set.stream().toList());
    return converter.toSimplesDtoList(list);
  }
}
